﻿using System;
using System.IO;
using System.Net;
using System.Text;

namespace UniversalApproach.Helpers
{
    public class HttpHelper
    {
        /// <summary>
        /// Get方式获取地址信息
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string HttpGet(string url)
        {
            var webRequest = WebRequest.Create(url) as HttpWebRequest;

            webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.42000)";
            webRequest.Method = "Get";
            webRequest.ContentType = "application/json";
            webRequest.Timeout = 15000;
            webRequest.AllowAutoRedirect = false;

            string result = null;
            try
            {
                WebResponse response = webRequest.GetResponse();
                bool flag2 = response != null;
                if (flag2)
                {
                    StreamReader streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    result = streamReader.ReadToEnd();
                    streamReader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
            }
            return result;
        }


        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url">文件地址</param>
        /// <param name="filePath">保存路径</param>
        /// <returns></returns>
        public static bool DownloadFile(string url,string filePath)
        {
            var webRequest = WebRequest.Create(url) as HttpWebRequest;

            webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.42000)";
            webRequest.Method = "Get";
            webRequest.ContentType = "application/octet-stream";
            webRequest.Timeout = 15000;
            webRequest.AllowAutoRedirect = false;

            //string result = null;
            try
            {
                HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse;
                bool flag2 = response != null;
                if (flag2)
                {
                    Stream streamReader = response.GetResponseStream();
                    FileStream fs = new FileStream(filePath, FileMode.Create);
                    byte[] bytes = new byte[1024];
                    int l;
                    do
                    {
                        l = streamReader.Read(bytes, 0, bytes.Length);
                        if (l > 0)
                            fs.Write(bytes, 0, l);
                    }
                    while (l > 0);
                    fs.Flush();
                    fs.Close();
                    streamReader.Close();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
            }
            return true;
        }
    }
}